<?php
/**
* 2013-5-6
*/
class bizhelper{
	
	/**
	* 2013-5-6
	*/
	function get_pm_count($uname,$state=0){
		global $db;
		return $db->fetch_value("select count(0) from tuserpersonalmessage where ToUserName='$uname' and State=$state");
	}
	
	function get_generalvalue($name){
		global $db;
		return htmlspecialchars_decode($db->fetch_value("select `Value` from tgeneraldata where `Name`='$name'"));
	}

	function update_generalvalue($name,$value,$tag){
		global $db;
		if($db->fetch_value("select count(0) from tgeneraldata where `Name`='$name'")==0){
			$db->execsql("insert into tgeneraldata (`Name`, `Value`, `ExTags`) values ('$name','$value','$tag')");	
		}else{
			$db->execsql("update tgeneraldata set `Value`='$value',  ExTags='$tag' where `Name`='$name';");
		}
	}
        
        /**
         * 
         * @global type $db
         * @param type $name
         * @param type $tmp %s=URL,%s=Picture,%s=Title
         * @return type
         */
	function get_ads_content($name,$tmp='<a href="%s"><img src="%s" alt="%s" /></a>'){
		global $db;
		$rs=$db->fetch_all("select a.Url, a.Picture, a.Title from tgeneraladvertisement a left join tgeneraladvertisement b on a.ParentID=b.ID where b.Title='$name' order by a.Ordinal, a.CreatedTime desc");
		$return='';
		if($rs){
			foreach($rs as $row){
				$return.=sprintf($tmp,$row['Url'],$row['Picture'],$row['Title']);	
			}
		}
		return $return;
	}
	
	function get_generaltree($val,$table,$tag='',$fields='ID,Name'){
		if(!$fields) $fields='ID, Name';
		global $db;
		$sqltag=$tag?" and ExTags='$tag'":'';
		$rs=mysql_query("select $fields from $table where ParentID=0 $sqltag");
		$str='<option value=\'0\'>请选择</option>';$chk='';
		while($row=mysql_fetch_array($rs)){
			$chk='';
			if($val==$row[0]) $chk=' selected=\'selected\'';
			$str=$str. '<option value=\''.$row[0].'\''.$chk.'>'.$row[1].'</option>';
			$rs2=mysql_query("select ID, Name from $table where ParentID=".$row[0]);
			if(!$rs2) continue;
			while($row2=mysql_fetch_array($rs2)){
				$chk='';
				if($val==$row2[0]) $chk=' selected=\'selected\'';
				$str=$str. '<option value=\''.$row2[0].'\''.$chk.'>--'.$row2[1].'</option>';			
			}
		}
		return $str;
	}
	
	/**
	* 获取会员资金账户信息 2013-5-6 by sutroon
	*/
	function get_accountinfo($uname){
		global $db;
		$rs=$db->fetch_row("select Blance, FrozenBlance,State from tfinanceaccount where UserName='$uname'");
		if(!$rs) $db->execsql(sprintf('insert into tfinanceaccount (UserName,Blance,FrozenBlance,Remark,`State`) values (\'%s\',0,0,\'系统查询创建账户\',1);',$uname));
		return $rs?$rs:array('Blance'=>0,'FrozenBlance'=>0,'State'=>1);
	}
	
	/**
	* 获取订单数量 2013-5-6 by sutroon
	*/
	function get_order_count($uname,$state=0,$isbuyer=true){
		global $db;
		$sqlc=' where ';
		if($uname) $sqlc.=($isbuyer?'BuyerName':'SellerName').'=\''.$uname.'\' and ';
		$sqlc.=' State='.$state;
		return $db->fetch_value('select count(0) from torderorder '.$sqlc);
	}
	
	/**
	* 获取商品数量 2013-5-6 by sutroon
	*/
	function get_product_count($uname,$state=-1){
		global $db;
		$sqlstate = $state==-1?'':" and State=$state ";
		return $db->fetch_value("select count(0) from tproductgoods where UserName='$uname' $sqlstate");
	}
	
	/**
	* 获取购物车总价格 2013-5-14 by sutroon
	*/
	function get_shoppingcar_amount($arr){
		if(!is_array($arr)) return 0;
		global $db;
		$rs=$db->fetch_all('select ID,Price from tproductgoods where ID in ('.implode(',',array_keys($arr)).');');
		if(!$rs) return 0;
		$amount=0;
		foreach($rs as $row){
			$amount+=$row[1]*$arr[$row[0]];
		}
		return $amount;
	}
	
	function get_state_name($state,$modul=''){
		switch(strtolower($modul)){
			case 'order':
				switch($state){
					case 0:
						return '未付款';
					case 100:
						return '未发货'; // 货到付款未发货
					case 110:
						return '已付款,未发货'; // 已付款未发货
					case 200:
						return '已发货';
					case 300:
						return '已签收';
					case 400:
						return '订单取消'; // 买家取消订单
					case 410:
						return '订单取消'; // 卖家取消订单
					case 500:
						return '交易完成';
				}
				break;
			case 'recharge':
			case 'withdrawal':
				switch($state){
					case 0:
						return '未审核';
					case 1:
						return '审核通过';
					case 4:
						return '审核未通过';
					default:
						return '状态异常';	
				}
				break;
                        case 'yesno':
                            return $state=='1'?'是':'否';
			default:
				switch($state){
					case 0:
						return '禁用';
					case 1:
						return '正常';
					default:
						return '异常';
				}
				break;	
		}
	}
	function get_type_name($type,$modul=''){
		switch($modul){
			case 'transaction':
				switch($type){
					case 'OUT_TRANSACTION':
						return '购物支付';
					case 'OUT_AA':
						return 'AA付款';
					case 'OUT_TRANSFER':
						return '转账支出';
					case 'OUT_WITHDRAWAL':
						return '提现扣款';
					case 'OUT_RENEWAL':
						return '续费扣款';
					case 'OUT_PREORDER_SEAT':
						return '预购席位扣款';
					case 'OUT_WINNING':
						return '中奖奖励支出';
					case 'IN_TRANSACTION':
						return '交易收入';
					case 'IN_AA':	
						return 'AA收款';
					case 'IN_TRANSFER':
						return '转账收入';
					case 'IN_RECHARGE':
						return '充值收款';
					case 'IN_RENEWAL':
						return '续费收款';
					case 'IN_PREORDER_SEAT':
						return '预购席位收款';
					case 'IN_PREORDER_SEAT_COMMISSION':
						return '预购席位提成收款';
					case 'IN_COMMISSION':
						return '提成收入';
					case 'IN_WINNING':
						return '中奖奖励收入';
					default:
						return '未知类型';
				}
				break;	
		}
	}
        
        /**
         * 添加一条交易(直接操作可用余额)
         * @global type $db
         * @param type $uname
         * @param type $amount
         * @param type $transtype
         * @return string
         */
	function add_transaction($uname,$amount,$transtype){
            if(strlen($transtype)==0) return '';
            $acc=$this->get_accountinfo($uname);
            if(substr($transtype,0,3)=='IN_'){
                    $acc['Blance']=$acc['Blance']+$amount;
            }else{
                    $acc['Blance']=$acc['Blance']-$amount;
                    if($acc['Blance']<0) return '余额不足';
            }
            global $db;
            $db->insert('tfinancetransactionlog','SerialNo,Amount,TransactionType,Blance,UserName,State,ModifiedTime',sprintf("'%s',%u,'%s',%u,'%s',1,'%s'",get_serialstring(),$amount,$transtype,$acc['Blance'],$uname,date('Y-m-d H:i:s')));
            $db->update('tfinanceaccount',sprintf("Blance=%u, ModifiedTime='%s'",$acc['Blance'],date('Y-m-d H:i:s')),"UserName='$uname'");
            return 'success';
        }
        
        /**
         * 预购席位付款
         * @global type $db
         * @param type $uname
         * @param type $amount
         * @return string
         */
        function add_prevorderseattransaction($uname,$amount){            
            $acc=$this->get_accountinfo($uname);
            $acc['Blance']=$acc['Blance']-$amount; 
            if($acc['Blance']<0) return '余额不足';
            global $db;
            $db->insert('tfinancetransactionlog','SerialNo,Amount,TransactionType,Blance,UserName,State,ModifiedTime',sprintf("'%s',%u,'OUT_PREORDER_SEAT',%u,'%s',1,'%s'",get_serialstring(),$amount,$acc['Blance'],$uname,date('Y-m-d H:i:s')));
            $db->update('tfinanceaccount',sprintf("Blance=Blance-%u,AdvancePayment=AdvancePayment+%u,ModifiedTime='%s'",$amount,$amount,date('Y-m-d H:i:s')),"UserName='$uname'");
            return 'success';
        }
        
        /**
         * 激活预购席位
         * @global type $db
         * @param type $uname
         * @param type $amount
         * @return string
         */
        function add_activeorderseattransaction($uname,$amount){
            global $db;
            $db->update('tfinanceaccount',sprintf("AdvancePayment=AdvancePayment-%u, ModifiedTime='%s'",$amount,date('Y-m-d H:i:s')),"UserName='$uname'");
            return 'success';            
        }
}
?>